package cn.mapway.tools.rpc.model;

import lombok.extern.slf4j.Slf4j;

/**
 * Types
 *
 * @author zhangjianshe@gmail.com
 */
@Slf4j
public enum Types {

    TYPE_INT("int", "int", "INT"),
    TYPE_INTEGER("Integer", "int", "INT"),
    TYPE_FLOAT("float", "Double", "INT"),
    TYPE_FLOAT_BOX("Float", "Double", "INT"),
    TYPE_DOUBLE("double", "Double", "INT"),
    TYPE_DOUBLE_BOX("Double", "Double", "INT"),
    TYPE_STRING("String", "String", "INT"),
    TYPE_BigNumber("BigNumber", "Double", "INT"),
    TYPE_LONG("long", "long", "INT"),
    TYPE_LONG_BOX("Long", "long", "INT"),
    TYPE_SHOTR("short", "int", "INT"),
    TYPE_SHORT_BOX("Short", "int", "INT"),
    TYPE_CHAR("Char", "int", "INT"),
    TYPE_CHAR_BOX("char", "int", "INT"),
    TYPE_BYTE("Byte", "int", "INT"),
    TYPE_BYTE_BOX("byte", "int", "INT"),
    TYPE_DATE("Date", "long", "INT");


    String javaName;
    String jsName;
    String sqlName;

    Types(String javaName, String jsName, String sqlName) {
        this.jsName = jsName;
        this.javaName = javaName;
        this.sqlName = sqlName;
    }

    public static boolean contains(String javaName) {
        for (Types t : Types.values()) {
            if (t.javaName.equals(simpleType(javaName))) {
                return true;
            }
        }
        return false;
    }

    public String findJs(String javaName) {
        for (Types t : Types.values()) {
            if (t.javaName.equals(simpleType(javaName))) {
                return t.jsName;
            }
        }
        log.warn("can not find javaName {}'s js type", javaName);
        return "String";
    }
    public static String simpleType(String type) {
        int index = type.lastIndexOf(".");
        if (index >= 0) {
            return type.substring(index + 1);
        }
        return type;
    }
}
